package org.samcolon.eeexplore.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description: 自定义合并单元格的策略
 * @author: Sam.zhang
 * @date: 2021.07.21
 */
public class MyMergeStrategy extends AbstractMergeStrategy {
    //合并坐标集合
    private List<CellRangeAddress> cellRangeAddresss;

    //构造
    public MyMergeStrategy(List<CellRangeAddress> cellRangeAddresss) {
        this.cellRangeAddresss = cellRangeAddresss;
    }

    /**
     * merge
     *
     * @param sheet
     * @param cell
     * @param head
     * @param relativeRowIndex
     */
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        //合并单元格
        /**
         *  ****加个判断:if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 0) {}****
         * 保证每个cell被合并一次，如果不加上面的判断，因为是一个cell一个cell操作的，
         * 例如合并A2:A3,当cell为A2时，合并A2,A3，但是当cell为A3时，又是合并A2,A3，
         * 但此时A2,A3已经是合并的单元格了
         */

        if (CollUtil.isNotEmpty(cellRangeAddresss)) {
            if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 0) {
                for (CellRangeAddress item : cellRangeAddresss) {
                    sheet.addMergedRegionUnsafe(item);
                }
            }
        }
    }

    private static List<CellRangeAddress> getCellRangeAddresss() {
        List<CellRangeAddress> list = new ArrayList<>();
        //合并第4行
        CellRangeAddress item1 = new CellRangeAddress(3, 3, 0, 3);
        //合并第第6行的第一列和第二列
        CellRangeAddress item2 = new CellRangeAddress(5, 5, 0, 1);
        //合并第9行和第10行
        CellRangeAddress item3 = new CellRangeAddress(10, 11, 0, 3);

        list.add(item1);
        list.add(item2);
        list.add(item3);
        return list;

    }

    public static void main(String[] args) {
//        //定义合并单元格的坐标范围
//        List<CellRangeAddress> cellRangeAddresss = getCellRangeAddresss();
//        //定义合并单元格策略
//        MyMergeStrategy myMergeStrategy = new MyMergeStrategy(cellRangeAddresss);
//
//        //写入的文excel文件
////        String fileName = "I:\\temp\\writeDemo4.xlsx";
//        File file = FileUtil.file(FileUtil.getUserHomeDir(), "test-merge-easyexcel.xlsx");
//
//        /*------------------------------分割线------------------------------*/
//        //这里自定义一个单元格的格式(标黄的行高亮显示)
//        Integer[] yellowRows = {3, 5, 7, 9};
//        Set<Integer> yellowRowsSet = CollUtil.newHashSet(yellowRows);
//        CustomCellWriteHandler customCellWriteHandler = new CustomCellWriteHandler(yellowRowsSet);
//
//        //获取头和内容的策略
//        HorizontalCellStyleStrategy horizontalCellStyleStrategy = getHorizontalCellStyleStrategy();
//
//        //列宽的策略,宽度是小单位
//        Integer columnWidthArr[] = {3000, 3000, 2000, 6000};
//        List<Integer> columnWidths = CollUtil.newArrayList(columnWidthArr);
//        CustomSheetWriteHandler customSheetWriteHandler = new CustomSheetWriteHandler(columnWidths);
//
//        // 根据自定义导出的字段
//        String[] filds = {"uid", "name", "age", "birthday"};
//
//        String[] headers = {"唯一识别码", "姓名", "年龄", "生日"};
//
//        List head = getHeadByFilds(headers);
//
//        //获取模拟的实体数据集合
//        List<User> userList = getUserList();
//
//
//        //这里指定头的名字去写入，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        EasyExcel.write(file)
//                .head(head)
//                .registerWriteHandler(horizontalCellStyleStrategy)
//                .registerWriteHandler(customSheetWriteHandler)
//                //注册单元格式
//                .registerWriteHandler(customCellWriteHandler)
//                //注册合并策略
//                .registerWriteHandler(myMergeStrategy)
//                .includeColumnFiledNames(CollUtil.newArrayList(filds))
//                .sheet("模板")
//                .doWrite(userList);
    }
}
